{extend name="public:base"}
{block name="body"}
<body class="pear-container">
	<div class="layui-card">
		<div class="layui-card-body">
			<form class="layui-form" action="">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keywords" placeholder="用户名|手机号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">登录IP</label>
                    <div class="layui-input-inline">
                        <input type="text" name="last_login_ip" placeholder="最后登录IP" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">登录日期</label>
                    <div class="layui-inline" id="login_date">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="start_date" id="start_date"
                                class="layui-input" readonly placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="end_date" id="end_date" class="layui-input"
                                readonly placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value="">选择状态</option>
                            <option value="1">启用</option>
                            <option value="0">禁用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
			</form>
		</div>
	</div>
	<div class="layui-card">
		<div class="layui-card-body">
			<table id="table-list" lay-filter="table-list"></table>
		</div>
	</div>
</body>
{/block}
{block name="custome"}
	<script type="text/html" id="table-toolbar">
		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
		<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
			<i class="layui-icon layui-icon-delete"></i>
			删除
		</button>
	</script>
	<script type="text/html" id="table-bar">
		
        {{#  if(d.id > 1 ){ }}
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
        {{# } }}
	</script>
	{/block}
{block name="script"}
<script type="text/html" id="table-status">
        {{#  if(d.id == 1 ){ }}
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"  checked disabled />
        {{# } }}
        {{#  if(d.id > 1 ){ }}
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="table-status" {{ d.status== 1 ? 'checked' : '' }} />
        {{# } }}
    </script>
<script type="text/html" id="user-group">
        {{#  if(d.id == 1 ){ }}
        <span class="layui-badge layui-bg-blue">超级管理员</span>
        {{# } else { }}

            {{# let tags = d.group?d.group.split(','):''}}
            {{# if (tags != ''){ }}
            {{# layui.each(tags, function(index, item){ }}
            <span class="layui-badge layui-bg-blue">{{ item }}</span>
            {{# }) }}
            {{# } }}
        {{# } }}
    </script>
<script type="text/html" id="user-last_login_time">
        {{layui.util.toDateString(parseInt(d.last_login_time)*1000, 'yyyy-MM-dd HH:mm')}}
    </script>
<script type="text/html" id="user-create_time">
        {{layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm')}}
    </script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function () {
        let laydate = layui.laydate;
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let cols = [
            [{
                type: 'checkbox'
            },
            {
                title: '用户名',
                field: 'username',
                align: 'center',
                width: 200
            },
            {
                title: '手机号',
                field: 'mobile',
                align: 'center',
                width: 200
            },
            {
                title: '最后登录时间',
                field: 'last_login_time',
                align: 'center',
                templet: '#user-last_login_time'
            },
            {
                title: '最后登录IP',
                field: 'last_login_ip',
                align: 'center'
            },
            {
                title: '角色',
                field: 'group',
                align: 'center',
                templet: '#user-group'
            },
            {
                title: '创建时间',
                field: 'create_time',
                align: 'center',
                templet: '#user-create_time'

            },
            {
                title: '状态',
                field: 'status',
                align: 'center',
                templet: '#table-status'
            },
            {
                title: '操作',
                toolbar: '#table-bar',
                align: 'center',
                width: 130
            }
            ]
        ]

        table.render({
            elem: '#table-list',
            url: '{:url("index")}',
            parseData: function(res){ 
            return {
            "code": res.code, //解析接口状态
            "msg": res.message, //解析提示文本
            "count": res.data.total, //解析数据长度
            "data": res.data.data //解析数据列表
            }},
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#table-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });
        form.on('submit(table-query)', function (data) {
            table.reload('table-list', {
                where: data.field
            })
            return false;
        });
        table.on('tool(table-list)', function (obj) {
            if (obj.event === 'remove') {
                common.remove(obj,"{:url('batch')}");
            }
            if (obj.event === 'edit') {
                common.modalo('{:url("edit")}?ids=' + obj.data['id'],'编辑');
            }
        });

        table.on('toolbar(table-list)', function (obj) {
            if (obj.event === 'add') {
               common.modalo('{:url("add")}','新增');
            } else if (obj.event === 'refresh') {
                common.refresh();
            } else if (obj.event === 'batchRemove') {
                common.batchRemove(obj,"{:url('batch')}");
            }
        });

        laydate.render({
            elem: '#login_date'
            , range: ['#start_date', '#end_date']
        });
        form.on('switch(table-status)', function (data) {
            let loading = layer.load();
            let field = {};
            field['id']=data.value;
            field[data.elem.name] = data.elem.checked?"1":"0";
            $.ajax({
                url: '{:url("change")}',
                type: 'post',
                data: field,
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg('操作成功', {
								icon: 1,
								time: 1000
							})
                    } else {
                        layer.msg(result.msg, {
								icon: 2,
								time: 1000
							})
                    }
                }
            })
        });  

        
    })
</script>
{/block}